<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var query = '{{.query}}';
var products_id = '{{.products_id}}';
var _select_row = 0;
var _max_row = 0;
var dataTables1 = $('#dataTables').DataTable({
    "sAjaxSource" : "{{ urlfor "AllocationController.SelectErpInventory"}}?allocation_id="+parent.allocation_id
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        params.orFilter = $('#isfoucs').val() ?  $('#isfoucs').val() : query;
        params.products_id = Number(products_id);
        $.post(sSource, JSON.stringify(params),function(resp) {
            _max_row = resp.iTotalDisplayRecords;
            fnCallback(resp);
        },'json')
    }
    ,order:[]
    ,columns: [
        {"bSortable": false, render:function(data, type, full, meta) {
                return productGroupName(full.Products);
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.sph;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.cyl;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.add;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.model;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.color;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.lot_number;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.manufacture_date > 0 ? timetransDate(full.Acceptance.manufacture_date) : '';
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.expiry_date > 0 ? timetransDate(full.Acceptance.expiry_date) : '';
            }}
        ,{"data" : "purchase_num","bSortable": false, render:function(data, type, full, meta) {
                return data;
            }}
    ]
    ,"aLengthMenu":[15, 50, 100, 200]
    ,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    ,"drawCallback": function(settings){
        $("#dataTables").find('tbody > tr').eq(_select_row).addClass("am-active");
        $("#dataTables").find('tbody > tr').off('dblclick').on('dblclick', function () {
            confirmSelected();
        });
    }
});
$('#search-btn').click(function(){
    $("#dataTables").dataTable().fnDraw(false);
});
$(function(){
    $('#isfoucs').val(query).focus();
    document.onkeydown = function (e) {
        if (!e) e = window.event;
        if ((e.keyCode || e.which) == 38) {  //向上
            _select_row--;
            if(_select_row<=0) {
                _select_row = 0;
            }
        }
        if ((e.keyCode || e.which) == 40) {  //向下
            _select_row++;
            if(_select_row >= _max_row ) {
                _select_row = _max_row-1;
            }
        }
        $('#infocus').focus();
        $("#dataTables").find('tbody > tr').removeClass('am-active');
        $("#dataTables").find('tbody > tr').eq(_select_row).addClass("am-active");
        if ((e.keyCode || e.which) == 13) {  //确认选择
            confirmSelected();
            return false;
        }
    }
});
//确认选择
function confirmSelected() {
    var _data = [];
    var datas = $('#dataTables').DataTable().row(_select_row).data();
    _data.push(datas);
    parent.addGoods(_data);
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
</script>